package com.example.wan1.view.system.website;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.wan1.R;
import com.example.wan1.base.fragment.SimpleFragment;
import com.example.wan1.model.WebsiteBean;
import com.example.wan1.view.web.JumpTool;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import androidx.core.content.ContextCompat;
import butterknife.BindView;
import mao.com.flexibleflowlayout.TagAdapter;
import mao.com.flexibleflowlayout.TagFlowLayout;

import static androidx.core.content.res.ResourcesCompat.getDrawable;


/***
 * 创建时间：2020/7/8 21:47
 * 创建人：10850
 * 功能描述：
 */
public class WebsiteFragment extends SimpleFragment implements WebsiteContract.View {

    @BindView(R.id.website_fl)
    TagFlowLayout mWebsiteFl;
    @Inject
    List<WebsiteBean> mList;
    @Inject
    WebsitePresenter mPresenter;

    @Override
    protected void initData() {
        //Log.i("zxd", "initData: ");
        mPresenter.getWebsiteData();
    }

    @Override
    protected void initView() {
        showLoading();
        mList = new ArrayList<>();
        //do nothing
    }

    @Override
    protected int initLayoutRes() {
        return R.layout.fragment_website;
    }

    @Override
    public void onSuccess(List<WebsiteBean> data) {
        hideLoading();
        mList.addAll(data);
        mWebsiteFl.setAdapter(new TagAdapter() {
            @Override
            public int getItemCount() {
                return data.size();
            }

            @Override
            public View createView(LayoutInflater inflater, ViewGroup parent, int position) {
                return inflater.inflate(R.layout.website_item, parent, false);
            }

            @Override
            public void bindView(View view, final int position) {
                TextView tv = view.findViewById(R.id.website_tv);
                tv.setText(data.get(position).getName());
                //tv.setBackgroundColor(Color.RED);
                if (position % 3 == 0) {
                    //https://www.cnblogs.com/whycxb/p/6851583.html
                    //https://blog.csdn.net/lvshuchangyin/article/details/52523465
                    //之前的写法已经淘汰了，getDrawable(R.drawable.tag_one)
                    tv.setBackground(getDrawable(getResources(), R.drawable.tag_one, null));
                    //tv.setBackground(ContextCompat.getDrawable(mContext, R.drawable.tag_one));
                } else if (position % 3 == 1) {
                    tv.setBackground(ContextCompat.getDrawable(mContext, R.drawable.tag_two));
                } else {
                    tv.setBackground(ContextCompat.getDrawable(mContext, R.drawable.tag_three));
                }
            }

            @Override
            public void onTagItemViewClick(View v, final int position) {
                super.onTagItemViewClick(v, position);
                switch (v.getId()) {
                    case R.id.website_tv:
                        JumpTool.goWeb(mContext, data.get(position).getName(), data.get(position).getLink());
                        break;
                }
            }

            @Override
            public void onItemSelected(View v, final int position) {
                super.onItemSelected(v, position);
                switch (v.getId()) {
                    case R.id.website_tv:
                        TextView tv = v.findViewById(R.id.website_tv);
//                            tv.setBackgroundColor(Color.RED);
                        tv.setBackground(ContextCompat.getDrawable(mContext, R.drawable.tag_four));
                        break;
                }

            }

            @Override
            public void onItemUnSelected(View v, final int position) {
                super.onItemUnSelected(v, position);
                TextView tv = v.findViewById(R.id.website_tv);
                if (position % 3 == 0) {
                    tv.setBackground(getDrawable(getResources(), R.drawable.tag_one, null));
                } else if (position % 3 == 1) {
                    tv.setBackground(ContextCompat.getDrawable(mContext, R.drawable.tag_two));
                } else {
                    tv.setBackground(ContextCompat.getDrawable(mContext, R.drawable.tag_three));
                }
            }
        });
    }

    @Override
    public void onFailure(String msg) {
        showError();
    }
}